﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace el.DAL
{
	/// <summary>
	/// 数据访问类:Sys_Admin
	/// </summary>
	public partial class Sys_Admin
	{
		public Sys_Admin()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("UID", "Sys_Admin"); 
		}

		/// <summary>
		/// 是否存在该记录

		/// </summary>
		public bool Exists(int UID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Sys_Admin");
			strSql.Append(" where UID=@UID");
			SqlParameter[] parameters = {
					new SqlParameter("@UID", SqlDbType.Int,4)
};
			parameters[0].Value = UID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据

		/// </summary>
		public int Add(el.Model.Sys_Admin model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Sys_Admin(");
			strSql.Append("RoleID,UserName,Password,Email,Mobile,CreateBy,CreateDate,LastUpdateBy,LastUpdateDate,IsDelete)");
			strSql.Append(" values (");
			strSql.Append("@RoleID,@UserName,@Password,@Email,@Mobile,@CreateBy,@CreateDate,@LastUpdateBy,@LastUpdateDate,@IsDelete)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateBy", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateBy", SqlDbType.Int,4),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Int,4)};
			parameters[0].Value = model.RoleID;
			parameters[1].Value = model.UserName;
			parameters[2].Value = model.Password;
			parameters[3].Value = model.Email;
			parameters[4].Value = model.Mobile;
			parameters[5].Value = model.CreateBy;
			parameters[6].Value = model.CreateDate;
			parameters[7].Value = model.LastUpdateBy;
			parameters[8].Value = model.LastUpdateDate;
			parameters[9].Value = model.IsDelete;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据

		/// </summary>
		public bool Update(el.Model.Sys_Admin model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Sys_Admin set ");
			strSql.Append("RoleID=@RoleID,");
			strSql.Append("UserName=@UserName,");
			strSql.Append("Password=@Password,");
			strSql.Append("Email=@Email,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("CreateBy=@CreateBy,");
			strSql.Append("CreateDate=@CreateDate,");
			strSql.Append("LastUpdateBy=@LastUpdateBy,");
			strSql.Append("LastUpdateDate=@LastUpdateDate,");
			strSql.Append("IsDelete=@IsDelete");
			strSql.Append(" where UID=@UID");
			SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateBy", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateBy", SqlDbType.Int,4),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Int,4),
					new SqlParameter("@UID", SqlDbType.Int,4)};
			parameters[0].Value = model.RoleID;
			parameters[1].Value = model.UserName;
			parameters[2].Value = model.Password;
			parameters[3].Value = model.Email;
			parameters[4].Value = model.Mobile;
			parameters[5].Value = model.CreateBy;
			parameters[6].Value = model.CreateDate;
			parameters[7].Value = model.LastUpdateBy;
			parameters[8].Value = model.LastUpdateDate;
			parameters[9].Value = model.IsDelete;
			parameters[10].Value = model.UID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据

		/// </summary>
		public bool Delete(int UID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Admin ");
			strSql.Append(" where UID=@UID");
			SqlParameter[] parameters = {
					new SqlParameter("@UID", SqlDbType.Int,4)
};
			parameters[0].Value = UID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Admin ");
			strSql.Append(" where UID in ("+UIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体

		/// </summary>
		public el.Model.Sys_Admin GetModel(int UID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UID,RoleID,UserName,Password,Email,Mobile,CreateBy,CreateDate,LastUpdateBy,LastUpdateDate,IsDelete from Sys_Admin ");
			strSql.Append(" where UID=@UID");
			SqlParameter[] parameters = {
					new SqlParameter("@UID", SqlDbType.Int,4)
};
			parameters[0].Value = UID;

			el.Model.Sys_Admin model=new el.Model.Sys_Admin();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UID"]!=null && ds.Tables[0].Rows[0]["UID"].ToString()!="")
				{
					model.UID=int.Parse(ds.Tables[0].Rows[0]["UID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RoleID"]!=null && ds.Tables[0].Rows[0]["RoleID"].ToString()!="")
				{
					model.RoleID=int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserName"]!=null && ds.Tables[0].Rows[0]["UserName"].ToString()!="")
				{
					model.UserName=ds.Tables[0].Rows[0]["UserName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Password"]!=null && ds.Tables[0].Rows[0]["Password"].ToString()!="")
				{
					model.Password=ds.Tables[0].Rows[0]["Password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mobile"]!=null && ds.Tables[0].Rows[0]["Mobile"].ToString()!="")
				{
					model.Mobile=ds.Tables[0].Rows[0]["Mobile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateBy"]!=null && ds.Tables[0].Rows[0]["CreateBy"].ToString()!="")
				{
					model.CreateBy=int.Parse(ds.Tables[0].Rows[0]["CreateBy"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastUpdateBy"]!=null && ds.Tables[0].Rows[0]["LastUpdateBy"].ToString()!="")
				{
					model.LastUpdateBy=int.Parse(ds.Tables[0].Rows[0]["LastUpdateBy"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastUpdateDate"]!=null && ds.Tables[0].Rows[0]["LastUpdateDate"].ToString()!="")
				{
					model.LastUpdateDate=DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsDelete"]!=null && ds.Tables[0].Rows[0]["IsDelete"].ToString()!="")
				{
					model.IsDelete=int.Parse(ds.Tables[0].Rows[0]["IsDelete"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UID,RoleID,UserName,Password,Email,Mobile,CreateBy,CreateDate,LastUpdateBy,LastUpdateDate,IsDelete ");
			strSql.Append(" FROM Sys_Admin ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据

		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UID,RoleID,UserName,Password,Email,Mobile,CreateBy,CreateDate,LastUpdateBy,LastUpdateDate,IsDelete ");
			strSql.Append(" FROM Sys_Admin ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Sys_Admin";
			parameters[1].Value = "UID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

